#include "ADC.h"

void initADC(void)
{
    _sadc0 = ADC0_FORMAT_12BIT + ADC0_OPA1O;
    _sadc1 = ADC1_VREF_AVDD + ADC1_CLK_FSYS_4;
    _enadc = 1;
    GCC_DELAY(10);
}

void DeInitADC(void)
{
    _enadc = 0;
}

void SelectADCChannel(uint8 channel)
{
    uint8 temp;

    temp = _sadc0;
    temp &= 0b11110000;
    temp |= channel;
    _sadc0 = temp;
}

uint16 ADConverter(void)
{
    uint16 result;

// P_TEST2 = HIGH;
    _start = 0;
    _start = 1;
    _start = 0;
    while(_eocb);
//     GCC_DELAY(20);
// P_TEST2 = LOW;
    result = _sadoh;
    result = result<<8;
    result |= _sadol;

    return result>>2;
}

uint16 GetADC(uint8 channel)
{
    uint16 result;
    // uint8 temp;

//     temp = _sadc0;
//     temp &= 0b11110000;
//     temp |= channel;
//     _sadc0 = temp;

// P_TEST2 = HIGH;
//     // START_ADC();
//     _start = 0;
//     _start = 1;
//     _start = 0;
//     while(_eocb);
// P_TEST2 = LOW;
//     result = _sadoh;
//     result = result<<8;
//     result |= _sadol;
    SelectADCChannel(channel);
    result = ADConverter();

    return result;

}